<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.10"/>
<title>Introduction_to_Algorithms: IntrodunctionToAlgorithm::SelectAlgorithm Namespace Reference</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
  $(document).ready(initResizable);
  $(window).load(resizeHeight);
</script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/javascript">
  $(document).ready(function() { init_search(); });
</script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectalign" style="padding-left: 0.5em;">
   <div id="projectname">Introduction_to_Algorithms
   </div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.10 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li class="current"><a href="namespaces.html"><span>Namespaces</span></a></li>
      <li><a href="annotated.html"><span>Classes</span></a></li>
      <li><a href="files.html"><span>Files</span></a></li>
      <li>
        <div id="MSearchBox" class="MSearchBoxInactive">
        <span class="left">
          <img id="MSearchSelect" src="search/mag_sel.png"
               onmouseover="return searchBox.OnSearchSelectShow()"
               onmouseout="return searchBox.OnSearchSelectHide()"
               alt=""/>
          <input type="text" id="MSearchField" value="Search" accesskey="S"
               onfocus="searchBox.OnSearchFieldFocus(true)" 
               onblur="searchBox.OnSearchFieldFocus(false)" 
               onkeyup="searchBox.OnSearchFieldChange(event)"/>
          </span><span class="right">
            <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
          </span>
        </div>
      </li>
    </ul>
  </div>
  <div id="navrow2" class="tabs2">
    <ul class="tablist">
      <li><a href="namespaces.html"><span>Namespace&#160;List</span></a></li>
      <li><a href="namespacemembers.html"><span>Namespace&#160;Members</span></a></li>
    </ul>
  </div>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
      <div id="nav-sync" class="sync"></div>
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
$(document).ready(function(){initNavTree('namespace_introdunction_to_algorithm_1_1_select_algorithm.html','');});
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div class="header">
  <div class="summary">
<a href="#func-members">Functions</a>  </div>
  <div class="headertitle">
<div class="title">IntrodunctionToAlgorithm::SelectAlgorithm Namespace Reference</div>  </div>
</div><!--header-->
<div class="contents">

<p>Namespace of <a class="el" href="namespace_introdunction_to_algorithm_1_1_select_algorithm.html" title="Namespace of SelectAlgorithm. ">SelectAlgorithm</a>.  
<a href="#details">More...</a></p>
<table class="memberdecls">
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a name="func-members"></a>
Functions</h2></td></tr>
<tr class="memitem:a1a12272ccfaf7b91916aed885085b86c"><td class="memTemplParams" colspan="2">template&lt;typename Iterator , typename Compare  = std::less&lt;typename std::iterator_traits&lt;Iterator&gt;::value_type&gt;&gt; </td></tr>
<tr class="memitem:a1a12272ccfaf7b91916aed885085b86c"><td class="memTemplItemLeft" align="right" valign="top">std::iterator_traits&lt; Iterator &gt;::value_type&#160;</td><td class="memTemplItemRight" valign="bottom"><a class="el" href="namespace_introdunction_to_algorithm_1_1_select_algorithm.html#a1a12272ccfaf7b91916aed885085b86c">good_select</a> (const Iterator begin, const Iterator end, typename std::iterator_traits&lt; Iterator &gt;::difference_type rank, Compare compare=Compare())</td></tr>
<tr class="memdesc:a1a12272ccfaf7b91916aed885085b86c"><td class="mdescLeft">&#160;</td><td class="mdescRight">good_select： 算法导论第9章9.3 最坏时间为O(n)的顺序统计量选择算法  <a href="#a1a12272ccfaf7b91916aed885085b86c">More...</a><br /></td></tr>
<tr class="separator:a1a12272ccfaf7b91916aed885085b86c"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:acb1b35e9cb2c25d81a69b5a1147504a4"><td class="memTemplParams" colspan="2">template&lt;typename IntType &gt; </td></tr>
<tr class="memitem:acb1b35e9cb2c25d81a69b5a1147504a4"><td class="memTemplItemLeft" align="right" valign="top">IntType&#160;</td><td class="memTemplItemRight" valign="bottom"><a class="el" href="namespace_introdunction_to_algorithm_1_1_select_algorithm.html#acb1b35e9cb2c25d81a69b5a1147504a4">radom_index</a> (IntType begin, IntType end)</td></tr>
<tr class="memdesc:acb1b35e9cb2c25d81a69b5a1147504a4"><td class="mdescLeft">&#160;</td><td class="mdescRight">radom_index： 生成随机整数  <a href="#acb1b35e9cb2c25d81a69b5a1147504a4">More...</a><br /></td></tr>
<tr class="separator:acb1b35e9cb2c25d81a69b5a1147504a4"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a08910fcecf3bf8711f109eec310f5a9b"><td class="memTemplParams" colspan="2">template&lt;typename Iterator , typename Compare  = std::less&lt;typename std::iterator_traits&lt;Iterator&gt;::value_type&gt;&gt; </td></tr>
<tr class="memitem:a08910fcecf3bf8711f109eec310f5a9b"><td class="memTemplItemLeft" align="right" valign="top">std::iterator_traits&lt; Iterator &gt;::value_type&#160;</td><td class="memTemplItemRight" valign="bottom"><a class="el" href="namespace_introdunction_to_algorithm_1_1_select_algorithm.html#a08910fcecf3bf8711f109eec310f5a9b">randomized_select</a> (const Iterator begin, const Iterator end, typename std::iterator_traits&lt; Iterator &gt;::difference_type rank, Compare compare=Compare())</td></tr>
<tr class="memdesc:a08910fcecf3bf8711f109eec310f5a9b"><td class="mdescLeft">&#160;</td><td class="mdescRight">randomized_select： 算法导论第9章9.2 顺序统计量的随机选择算法  <a href="#a08910fcecf3bf8711f109eec310f5a9b">More...</a><br /></td></tr>
<tr class="separator:a08910fcecf3bf8711f109eec310f5a9b"><td class="memSeparator" colspan="2">&#160;</td></tr>
</table>
<a name="details" id="details"></a><h2 class="groupheader">Detailed Description</h2>
<div class="textblock"><p>Namespace of <a class="el" href="namespace_introdunction_to_algorithm_1_1_select_algorithm.html" title="Namespace of SelectAlgorithm. ">SelectAlgorithm</a>. </p>
<p>该命名空间内包含所有顺序统计量算法 </p>
</div><h2 class="groupheader">Function Documentation</h2>
<a class="anchor" id="a1a12272ccfaf7b91916aed885085b86c"></a>
<div class="memitem">
<div class="memproto">
<div class="memtemplate">
template&lt;typename Iterator , typename Compare  = std::less&lt;typename std::iterator_traits&lt;Iterator&gt;::value_type&gt;&gt; </div>
      <table class="memname">
        <tr>
          <td class="memname">std::iterator_traits&lt;Iterator&gt;::value_type IntrodunctionToAlgorithm::SelectAlgorithm::good_select </td>
          <td>(</td>
          <td class="paramtype">const Iterator&#160;</td>
          <td class="paramname"><em>begin</em>, </td>
        </tr>
        <tr>
          <td class="paramkey"></td>
          <td></td>
          <td class="paramtype">const Iterator&#160;</td>
          <td class="paramname"><em>end</em>, </td>
        </tr>
        <tr>
          <td class="paramkey"></td>
          <td></td>
          <td class="paramtype">typename std::iterator_traits&lt; Iterator &gt;::difference_type&#160;</td>
          <td class="paramname"><em>rank</em>, </td>
        </tr>
        <tr>
          <td class="paramkey"></td>
          <td></td>
          <td class="paramtype">Compare&#160;</td>
          <td class="paramname"><em>compare</em> = <code>Compare()</code>&#160;</td>
        </tr>
        <tr>
          <td></td>
          <td>)</td>
          <td></td><td></td>
        </tr>
      </table>
</div><div class="memdoc">

<p>good_select： 算法导论第9章9.3 最坏时间为O(n)的顺序统计量选择算法 </p>
<dl class="params"><dt>Parameters</dt><dd>
  <table class="params">
    <tr><td class="paramname">begin</td><td>: 待选取的序列的起始迭代器（也可以是指向数组中某元素的指针） </td></tr>
    <tr><td class="paramname">end</td><td>待选取的序列的终止迭代器（也可以是指向数组中某元素的指针） </td></tr>
    <tr><td class="paramname">rank</td><td>指定选取的顺序数，0为选取最小的元素，1为选取次小的元素....n表示选取排序为n的元素（从小排序） </td></tr>
    <tr><td class="paramname">compare</td><td>一个可调用对象，可用于比较两个对象的小于比较，默认为std::less&lt;T&gt; </td></tr>
  </table>
  </dd>
</dl>
<dl class="section return"><dt>Returns</dt><dd>第rank小的元素值</dd></dl>
<ul>
<li>选择算法思想，假设对数组A[p...r]选择，选择第k小的元素：<ul>
<li>选择主元：<ul>
<li>首先将序列从前往后，按照5个元素一组分组。其中最后一组可能为1～5个元素（也就是可能不是满的）</li>
<li>然后将这些分组进行排序（我采用的是快速排序）</li>
<li>然后将这些分组中的中位数（即最中间的数）取出</li>
<li>针对这些分组的中位数构成的序列，递归调用 good_select，找出中位数的中位数</li>
<li>将这个中位数的中位数作为划分主元</li>
</ul>
</li>
<li>划分：根据找到的主元对原序列进行划分，假设划分完毕后，主元是第m小</li>
<li>判定：<ul>
<li>若m==k，则找到了这个元素，返回这个主元</li>
<li>若m&lt;k ，则说明指定的元素在 A[m+1...r]中，且位于这个新数组的第(k-m-1)小，此时递归调用good_select(....)</li>
<li>若m&gt;k， 则说明指定的元素在 A[p...m-1]中，且位于这个新数组的第 k 小，此时递归调用good_select(...)</li>
</ul>
</li>
</ul>
</li>
<li>时间复杂度：最坏情况下时间为O(n)</li>
<li>非原地操作：因为这里要把所有分组的中位数构造成一个序列，然后把找到该序列的中位数作为good_select(...)的主元 </li>
</ul>

<p>Definition at line <a class="el" href="goodselect_8h_source.html#l00034">34</a> of file <a class="el" href="goodselect_8h_source.html">goodselect.h</a>.</p>

</div>
</div>
<a class="anchor" id="acb1b35e9cb2c25d81a69b5a1147504a4"></a>
<div class="memitem">
<div class="memproto">
<div class="memtemplate">
template&lt;typename IntType &gt; </div>
      <table class="memname">
        <tr>
          <td class="memname">IntType IntrodunctionToAlgorithm::SelectAlgorithm::radom_index </td>
          <td>(</td>
          <td class="paramtype">IntType&#160;</td>
          <td class="paramname"><em>begin</em>, </td>
        </tr>
        <tr>
          <td class="paramkey"></td>
          <td></td>
          <td class="paramtype">IntType&#160;</td>
          <td class="paramname"><em>end</em>&#160;</td>
        </tr>
        <tr>
          <td></td>
          <td>)</td>
          <td></td><td></td>
        </tr>
      </table>
</div><div class="memdoc">

<p>radom_index： 生成随机整数 </p>
<dl class="params"><dt>Parameters</dt><dd>
  <table class="params">
    <tr><td class="paramname">begin</td><td>整数范围的开始数（生成的随机数在闭区间[begin,end]中） </td></tr>
    <tr><td class="paramname">end</td><td>整数范围的截止数（生成的随机数在闭区间[begin,end]中） </td></tr>
  </table>
  </dd>
</dl>
<dl class="section return"><dt>Returns</dt><dd>生成的随机数</dd></dl>
<p>这里采用[begin,end]之间的均匀分布 </p>

<p>Definition at line <a class="el" href="randomizedselect_8h_source.html#l00018">18</a> of file <a class="el" href="randomizedselect_8h_source.html">randomizedselect.h</a>.</p>

</div>
</div>
<a class="anchor" id="a08910fcecf3bf8711f109eec310f5a9b"></a>
<div class="memitem">
<div class="memproto">
<div class="memtemplate">
template&lt;typename Iterator , typename Compare  = std::less&lt;typename std::iterator_traits&lt;Iterator&gt;::value_type&gt;&gt; </div>
      <table class="memname">
        <tr>
          <td class="memname">std::iterator_traits&lt;Iterator&gt;::value_type IntrodunctionToAlgorithm::SelectAlgorithm::randomized_select </td>
          <td>(</td>
          <td class="paramtype">const Iterator&#160;</td>
          <td class="paramname"><em>begin</em>, </td>
        </tr>
        <tr>
          <td class="paramkey"></td>
          <td></td>
          <td class="paramtype">const Iterator&#160;</td>
          <td class="paramname"><em>end</em>, </td>
        </tr>
        <tr>
          <td class="paramkey"></td>
          <td></td>
          <td class="paramtype">typename std::iterator_traits&lt; Iterator &gt;::difference_type&#160;</td>
          <td class="paramname"><em>rank</em>, </td>
        </tr>
        <tr>
          <td class="paramkey"></td>
          <td></td>
          <td class="paramtype">Compare&#160;</td>
          <td class="paramname"><em>compare</em> = <code>Compare()</code>&#160;</td>
        </tr>
        <tr>
          <td></td>
          <td>)</td>
          <td></td><td></td>
        </tr>
      </table>
</div><div class="memdoc">

<p>randomized_select： 算法导论第9章9.2 顺序统计量的随机选择算法 </p>
<dl class="params"><dt>Parameters</dt><dd>
  <table class="params">
    <tr><td class="paramname">begin</td><td>: 待选取的序列的起始迭代器（也可以是指向数组中某元素的指针） </td></tr>
    <tr><td class="paramname">end</td><td>待选取的序列的终止迭代器（也可以是指向数组中某元素的指针） </td></tr>
    <tr><td class="paramname">rank</td><td>指定选取的顺序数，0为选取最小的元素，1为选取次小的元素....n表示选取排序为n的元素（从小排序） </td></tr>
    <tr><td class="paramname">compare</td><td>一个可调用对象，可用于比较两个对象的小于比较，默认为std::less&lt;T&gt; </td></tr>
  </table>
  </dd>
</dl>
<dl class="section return"><dt>Returns</dt><dd>第rank小的元素值</dd></dl>
<ul>
<li>选择算法思想，假设对数组A[p...r]选择，选择第k小的元素：<ul>
<li>随机选择主元：随机选取数组的一个下标q,A[q]作为划分元素</li>
<li>划分：利用A[q]划分数组，获得A[q]在序列中是第 m 小</li>
<li>判定：<ul>
<li>若m==k，则找到了这个元素，返回 A[q]</li>
<li>若m&lt;k ，则说明指定的元素在 A[q+1...r]中，且位于这个新数组的第(k-m-1)小，此时递归调用randomized_select(q+1,end,k-m-1)</li>
<li>若m&gt;k， 则说明指定的元素在 A[p...q-1]中，且位于这个新数组的第 k 小，此时递归调用randomized_select(begin,q,k)</li>
</ul>
</li>
</ul>
</li>
<li>时间复杂度：最坏情况下为O(n^2)，期望时间为O(n)</li>
<li>原地操作 </li>
</ul>

<p>Definition at line <a class="el" href="randomizedselect_8h_source.html#l00047">47</a> of file <a class="el" href="randomizedselect_8h_source.html">randomizedselect.h</a>.</p>

</div>
</div>
</div><!-- contents -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
  <ul>
    <li class="navelem"><a class="el" href="namespace_introdunction_to_algorithm.html">IntrodunctionToAlgorithm</a></li><li class="navelem"><a class="el" href="namespace_introdunction_to_algorithm_1_1_select_algorithm.html">SelectAlgorithm</a></li>
    <li class="footer">Generated by
    <a href="http://www.doxygen.org/index.html">
    <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.8.10 </li>
  </ul>
</div>
</body>
</html>
